/**
 * Copyright (c) 2014, 2017, Oracle and/or its affiliates.
 * The Universal Permissive License (UPL), Version 1.0
 */
"use strict";
/*
 Copyright 2013 jQuery Foundation and other contributors
 Released under the MIT license.
 http://jquery.org/license
*/
define(["ojs/ojcore","jquery","ojs/ojdatasource-common","ojs/ojmodel"],function(a,g){a.td=function(b,c){this.data={};if(!(b instanceof a.l))throw Error(a.W.od._ERR_DATA_INVALID_TYPE_SUMMARY+"\n"+a.W.od._ERR_DATA_INVALID_TYPE_DETAIL);a.td.u.constructor.call(this,b,c);this.ue=b;this.jpa();this.Init();if(null!=c&&("enabled"==c.startFetch||null==c.startFetch)||null==c)this.QE=!0};o_("CollectionTableDataSource",a.td,a);a.b.sa(a.td,a.W,"oj.CollectionTableDataSource");a.td.prototype.bm=null;a.b.g("CollectionTableDataSource.prototype.comparator",
{bm:a.td.prototype.bm});a.td.prototype.Init=function(){a.td.u.Init.call(this)};a.b.g("CollectionTableDataSource.prototype.Init",{Init:a.td.prototype.Init});a.td.prototype.at=function(b,c){c=c||{};c.deferred=!0;var d=this.ue.at(b,c),e=this;e.LD=!0;var f;return new Promise(function(c,g){null!=d?d.then(function(a){e.LD=!1;f={data:a.attributes,index:b,key:a.id};c(f)},function(b){e.LD=!1;a.W.u.handleEvent.call(e,a.W.O.ERROR,b);g(b)}):c(null)})};a.b.g("CollectionTableDataSource.prototype.at",{at:a.td.prototype.at});
a.td.prototype.fetch=function(a){a=a||{};return"init"!=a.fetchType||this.QE?this.Gg(a):Promise.resolve()};a.b.g("CollectionTableDataSource.prototype.fetch",{fetch:a.td.prototype.fetch});a.td.prototype.get=function(b,c){c=c||{};c.deferred=!0;var d=this.ue.get(b,c),e=this,f;return new Promise(function(b,c){null!=d?d.then(function(a){f={data:a.attributes,index:a.index,key:a.id};b(f)},function(b){a.W.u.handleEvent.call(e,a.W.O.ERROR,b);c(b)}):b(null)})};a.b.g("CollectionTableDataSource.prototype.get",
{get:a.td.prototype.get});a.td.prototype.sort=function(a){null==a?a=this.sortCriteria:this.sortCriteria=a;var c=this.comparator,d=this;return new Promise(function(e){null==c?(d.ue.comparator=a.key,d.ue.sortDirection="ascending"==a.direction?1:-1):d.ue.comparator=c;d.ue.sort(null);e({header:a.key,direction:a.direction})})};a.b.g("CollectionTableDataSource.prototype.sort",{sort:a.td.prototype.sort});a.td.prototype.totalSize=function(){var a=0<=this.ue.totalResults?this.ue.totalResults:-1;if(-1<a){var c=
this.ue.size();return c>a?c:a}if(0<this.QS)a=this.QS;else if("atLeast"==this.totalSizeConfidence())return this.ue.size();return a};a.b.g("CollectionTableDataSource.prototype.totalSize",{totalSize:a.td.prototype.totalSize});a.td.prototype.totalSizeConfidence=function(){return 0<=this.ue.totalResults?"actual":this.ue.hasMore?"atLeast":"unknown"};a.b.g("CollectionTableDataSource.prototype.totalSizeConfidence",{totalSizeConfidence:a.td.prototype.totalSizeConfidence});a.td.prototype.jpa=function(){var b=
this;this.ue.on(a.ka.O.SYNC,function(c){if(c instanceof a.l&&!b.LD&&!b.M$){var d=c.offset,e=c.lastFetchCount||c.lastFetchSize;0<e?(b.oa=d,b.wb=e,b.LD=!0,c.Fx(d,d+e).then(function(a){b.LD=!1;var c=[],e=[],g,m;for(g=0;g<a.length;g++)null!=a[g]&&(m=a[g].clone(),c.push(m.attributes),e.push(m.id));b.cn.call(b,{silent:!1},{data:c,keys:e,startIndex:d},null)})):(c=b.Xy(),b.cn.call(b,{silent:!1},c,null))}});this.ue.on(a.ka.O.ALLADDED,function(c,d){var e=[],f=[],g=[],k,l;for(k=0;k<d.length;k++)l=d[k].clone(),
e.push(l.attributes),f.push(l.id),g.push(l.index);a.W.u.handleEvent.call(b,a.W.O.ADD,{data:e,keys:f,indexes:g})});this.ue.on(a.ka.O.ALLREMOVED,function(c,d){var e=[],f=[],g=[],k,l;for(k=0;k<d.length;k++)l=d[k].clone(),e.push(l.attributes),f.push(l.id),g.push(l.index);a.W.u.handleEvent.call(b,a.W.O.REMOVE,{data:e,keys:f,indexes:g})});this.ue.on(a.ka.O.RESET,function(c){a.W.u.handleEvent.call(b,a.W.O.RESET,c)});this.ue.on(a.ka.O.SORT,function(c,d){if(null==d||!d.add){var e={};null==c||null==!c.comparator||
g.isFunction(c.comparator)||(e.header=c.comparator,e.direction=1===c.sortDirection?"ascending":"descending");a.W.u.handleEvent.call(b,a.W.O.SORT,e)}});this.ue.on(a.ka.O.CHANGE,function(c){a.W.u.handleEvent.call(b,a.W.O.CHANGE,{data:[c.attributes],keys:[c.id],indexes:[c.index]})});this.ue.on(a.ka.O.DESTROY,function(c){a.W.u.handleEvent.call(b,a.W.O.DESTROY,c)});this.ue.on(a.ka.O.REFRESH,function(c){a.W.u.handleEvent.call(b,a.W.O.REFRESH,c)});this.ue.on(a.ka.O.ERROR,function(c,d,e){a.W.u.handleEvent.call(b,
a.W.O.ERROR,c,d,e)})};a.td.prototype.Gg=function(a){this.PE(a);a=a||{};var c=this;this.X$=0<a.pageSize?!0:!1;this.oa=null==a.startIndex?this.oa:a.startIndex;this.wb=0<a.pageSize?a.pageSize:-1;a.pageSize=this.wb;a.startIndex=this.oa;a.refresh=!0;return new Promise(function(d,e){var f=c.wb;c.X$||(f=25);c.ue.vG(c.oa,f).then(function(e){var f;if(c.X$){f=[];var g=[],m,r;for(m=0;m<e.models.length;m++)r=e.models[m].clone(),f[m]=r.attributes,g[m]=r.id;f={data:f,keys:g,startIndex:c.oa};e.models.length<c.wb?
0>c.totalSize()&&(c.QS=c.oa+e.models.length):c.QS=null}else f=c.Xy();c.cn.call(c,a,f,null);d(f)},function(d){c.cn.call(c,a,null,d);e(d)})})};a.td.prototype.PE=function(b){this.M$=!0;b.silent||a.W.u.handleEvent.call(this,a.W.O.REQUEST,{startIndex:b.startIndex})};a.td.prototype.cn=function(b,c,d){this.M$=!1;null!=d?a.W.u.handleEvent.call(this,a.W.O.ERROR,d):b.silent||a.W.u.handleEvent.call(this,a.W.O.SYNC,c)};a.td.prototype.Xy=function(){var a=this.ue.size()-1,c=[],d=[],e,f,g;for(e=0;e<=a;e++)f=this.ue.at(e),
g=f.clone(),f=this.$l(g,g.attributes),c[e]=f,d[e]=g.id;return{data:c,keys:d,startIndex:this.oa}};a.td.prototype.getCapability=function(){return null};a.b.g("CollectionTableDataSource.prototype.getCapability",{getCapability:a.td.prototype.getCapability});a.td.prototype.$l=function(a,c){var d={},e;for(e in c)c.hasOwnProperty(e)&&function(){var c=e;Object.defineProperty(d,e,{get:function(){return a.get(c)},set:function(d){a.set(c,d,{silent:!0})},enumerable:!0})}();return d}});